$(function () {
    /*获取登录人姓名*/
    var username = $.cookie("name");
    if (username != null) {
        $("#login_user").html("账户名：" + username);
    }

    /*执行有效的查询条件的编号*/
    var indexLess = 1;
    var pageObjIndexLess = 1;
    /*执行有效的查询条件中正在审批的的编号*/
    var indexLessInExamination = 1;
    var pageObjIndexLessInExamination = 1;
    /*执行无效的查询条件的编号*/
    var indexGreater = 1;
    var pageObjIndexGreater = 1;
    /*页面一加载，执行有效的查询条件*/
    displayLess(1, 5);

    /**
     * 点击（正在审批按钮）按钮
     */
    $("#a_less_InExamination").click(function () {
        displayLessInExamination(1, 5);
    })

    /**
     * 点击（失效审批按钮）按钮
     */
    $("#a_greater").click(function () {
        displayGreater(1, 5);
    })

    //有效的查询条件
    function displayLess(pageNoStr, pageSizeStr) {
        /*执行ajax*/
        $.ajax({
            url: "../../ClinicalDataQuery/findByLessTime",
            data: {
                pageNoStr: pageNoStr,
                pageSizeStr: pageSizeStr,
            },
            type: "post",
            dataType: "json",
            success: function (map) {
                var htmlStr = "";
                var clinicalDataQueryList = map.clinicalDataQueryList;
                for (var i = 0; i < clinicalDataQueryList.length; i++) {
                    htmlStr += "<tr>"
                    htmlStr += "<td>" + indexLess + "</td>"
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/selectByClinicalDataQuery?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' >"
                    htmlStr += "" + clinicalDataQueryList[i].queryConditions + ""
                    htmlStr += "</a>"
                    htmlStr += "</td>"
                    var conditions = '';
                    if (clinicalDataQueryList[i].approverState == '1') {
                        conditions = "审批中";
                    } else if (clinicalDataQueryList[i].approverState == '2') {
                        conditions = "待审批";
                    }
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/selectApproval?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' >" + conditions + "</a>"
                    htmlStr += "</td>"
                    htmlStr += "<td>" + renderTime(clinicalDataQueryList[i].stateTime) + "</td>"
                    htmlStr += "<td>" + renderTime(clinicalDataQueryList[i].endTime) + "</td>"
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/updateByQueryConditionsId?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "'>"
                    htmlStr += "<i class='glyphicon glyphicon-dashboard' style='color: blue; padding: 0 10px;'></i>"
                    htmlStr += "</a>"
                    var confirmStr = "'确定删除(" + clinicalDataQueryList[i].queryConditions + ")吗？？？'";
                    htmlStr += "<a href='../../ClinicalDataQuery/deleteBy?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' onClick=\"return confirm(" + confirmStr + ");\" >"
                    htmlStr += "<i class='glyphicon glyphicon-trash' style='color: red; padding: 0 10px;'></i>"
                    htmlStr += "</a>"
                    htmlStr += "</td>"
                    htmlStr += "</tr>"
                    indexLess++;
                }
                //展示到页面上
                $("#page_less").html(htmlStr);
                /*页面插件-分页展示*/
                //计算总页数
                var totalPages = 1;
                if (map.countList % pageSizeStr == 0) {
                    totalPages = map.countList / pageSizeStr;
                } else {
                    totalPages = parseInt(map.countList / pageSizeStr) + 1;
                }
                if (pageObjIndexLess >= totalPages) {
                    indexLess = 1;
                }
                $("#query_page_less").bs_pagination({
                    currentPage: pageNoStr,	//当前页码
                    rowsPerPage: pageSizeStr,	//每页显示条数
                    totalRows: map.countList,	//总条数
                    totalPages: totalPages,//总页数
                    visiblePageLinks: 5,	//最多显示的卡片数
                    showGoToPage: true,		//是否显示跳转到几页
                    showRowsPerPage: true,	//是否显示每页显示条数
                    showRowsInfo: true,		//是否显示记录条数
                    //用来监听页号切换的事件，event代表事件，pageobj代表页面信息
                    onChangePage: function (event, pageObj) {
                        displayLess(pageObj.currentPage, pageObj.rowsPerPage);
                        pageObjIndexLess = pageObj.currentPage;
                    }
                });
            },
            error: function () {
                location.href = '404.jsp'
            }
        });
    }

    //有效的查询条件审核中
    function displayLessInExamination(pageNoStr, pageSizeStr) {
        /*执行ajax*/
        $.ajax({
            url: "../../ClinicalDataQuery/findByLessTimeInExamination",
            data: {
                pageNoStr: pageNoStr,
                pageSizeStr: pageSizeStr,
            },
            type: "post",
            dataType: "json",
            success: function (map) {
                var htmlStr = "";
                var clinicalDataQueryList = map.clinicalDataQueryList;
                for (var i = 0; i < clinicalDataQueryList.length; i++) {
                    htmlStr += "<tr>"
                    htmlStr += "<td>" + indexLessInExamination + "</td>"
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/selectByClinicalDataQuery?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' >"
                    htmlStr += "" + clinicalDataQueryList[i].queryConditions + ""
                    htmlStr += "</a>"
                    htmlStr += "</td>"
                    var conditions = '';
                    if (clinicalDataQueryList[i].approverState == '1') {
                        conditions = "审批中";
                    } else if (clinicalDataQueryList[i].approverState == '2') {
                        conditions = "待审批";
                    }
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/selectApproval?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' >" + conditions + "</a>"
                    htmlStr += "</td>"
                    htmlStr += "<td>" + renderTime(clinicalDataQueryList[i].stateTime) + "</td>"
                    htmlStr += "<td>" + renderTime(clinicalDataQueryList[i].endTime) + "</td>"
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/updateByQueryConditionsId?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "'>"
                    htmlStr += "<i class='glyphicon glyphicon-dashboard' style='color: blue; padding: 0 10px;'></i>"
                    htmlStr += "</a>"
                    var confirmStr = "'确定删除(" + clinicalDataQueryList[i].queryConditions + ")吗？？？'";
                    htmlStr += "<a href='../../ClinicalDataQuery/deleteBy?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' onClick=\"return confirm(" + confirmStr + ");\" >"
                    htmlStr += "<i class='glyphicon glyphicon-trash' style='color: red; padding: 0 10px;'></i>"
                    htmlStr += "</a>"
                    htmlStr += "</td>"
                    htmlStr += "</tr>"
                    indexLessInExamination++;
                }
                //展示到页面上
                $("#page_less_InExamination").html(htmlStr);
                /*页面插件-分页展示*/
                //计算总页数
                var totalPages = 1;
                if (map.countList % pageSizeStr == 0) {
                    totalPages = map.countList / pageSizeStr;
                } else {
                    totalPages = parseInt(map.countList / pageSizeStr) + 1;
                }
                if (pageObjIndexLessInExamination >= totalPages) {
                    indexLessInExamination = 1;
                }
                $("#query_page_less_InExamination").bs_pagination({
                    currentPage: pageNoStr,	//当前页码
                    rowsPerPage: pageSizeStr,	//每页显示条数
                    totalRows: map.countList,	//总条数
                    totalPages: totalPages,//总页数
                    visiblePageLinks: 5,	//最多显示的卡片数
                    showGoToPage: true,		//是否显示跳转到几页
                    showRowsPerPage: true,	//是否显示每页显示条数
                    showRowsInfo: true,		//是否显示记录条数
                    //用来监听页号切换的事件，event代表事件，pageobj代表页面信息
                    onChangePage: function (event, pageObj) {
                        displayLessInExamination(pageObj.currentPage, pageObj.rowsPerPage);
                        pageObjIndexLessInExamination = pageObj.currentPage;
                    }
                });
            },
            error: function () {
                location.href = '404.jsp'
            }
        });
    }

    //无效的查询条件
    function displayGreater(pageNoStr, pageSizeStr) {
        $.ajax({
            url: "../../ClinicalDataQuery/findByGreaterTime",
            data: {
                pageNoStr: pageNoStr,
                pageSizeStr: pageSizeStr,
            },
            type: "post",
            dataType: "json",
            success: function (map) {
                var htmlStr = "";
                var clinicalDataQueryList = map.clinicalDataQueryList;
                for (var i = 0; i < clinicalDataQueryList.length; i++) {
                    htmlStr += "<tr>"
                    htmlStr += "<td>" + indexGreater + "</td>"
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/selectByClinicalDataQuery?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' >"
                    htmlStr += "" + clinicalDataQueryList[i].queryConditions + ""
                    htmlStr += "</a>"
                    htmlStr += "</td>"
                    var conditions = '';
                    if (clinicalDataQueryList[i].approverState == '1') {
                        conditions = "审批中";
                    } else if (clinicalDataQueryList[i].approverState == '2') {
                        conditions = "待审批";
                    }
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/selectApproval?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' >" + conditions + "</a>"
                    htmlStr += "</td>"
                    htmlStr += "<td>" + renderTime(clinicalDataQueryList[i].stateTime) + "</td>"
                    htmlStr += "<td>" + renderTime(clinicalDataQueryList[i].endTime) + "</td>"
                    htmlStr += "<td>"
                    htmlStr += "<a href='../../ClinicalDataQuery/updateByQueryConditionsId?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "'>"
                    htmlStr += "<i class='glyphicon glyphicon-dashboard' style='color: blue; padding: 0 10px;'></i>"
                    htmlStr += "</a>"
                    var confirmStr = "'确定删除(" + clinicalDataQueryList[i].queryConditions + ")吗？？？'";
                    htmlStr += "<a href='../../ClinicalDataQuery/deleteBy?queryConditionsId=" + clinicalDataQueryList[i].queryConditionsId + "' onClick=\"return confirm(" + confirmStr + ");\" >"
                    htmlStr += "<i class='glyphicon glyphicon-trash' style='color: red; padding: 0 10px;'></i>"
                    htmlStr += "</a>"
                    htmlStr += "</td>"
                    htmlStr += "</tr>"
                    indexGreater++;
                }
                //展示到页面上
                $("#page_greater").html(htmlStr);
                /!*页面插件-分页展示*!/
                //计算总页数
                var totalPages = 1;
                if (map.countList % pageSizeStr == 0) {
                    totalPages = map.countList / pageSizeStr;
                } else {
                    totalPages = parseInt(map.countList / pageSizeStr) + 1;
                }
                if (pageObjIndexGreater >= totalPages) {
                    indexGreater = 1;
                }
                $("#query_page_greater").bs_pagination({
                    currentPage: pageNoStr,	//当前页码
                    rowsPerPage: pageSizeStr,	//每页显示条数
                    totalRows: map.countList,	//总条数
                    totalPages: totalPages,//总页数
                    visiblePageLinks: 5,	//最多显示的卡片数
                    showGoToPage: true,		//是否显示跳转到几页
                    showRowsPerPage: true,	//是否显示每页显示条数
                    showRowsInfo: true,		//是否显示记录条数
                    //用来监听页号切换的事件，event代表事件，pageobj代表页面信息
                    onChangePage: function (event, pageObj) {
                        displayGreater(pageObj.currentPage, pageObj.rowsPerPage);
                        pageObjIndexGreater = pageObj.currentPage;
                    }
                });
            },
            error: function () {
                location.href = '../../404.jsp'
            }
        });
    }

    /**
     *转换日期格式问题
     * @param time
     * @returns {string}
     */
    function renderTime(date) {
        var dateee = new Date(date).toJSON();
        return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    }

})